package com.doudou.casebook.utils;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.os.Build;
import android.text.Editable;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.doudou.casebook.R;
import com.google.android.material.snackbar.Snackbar;

public class KeyBoardUtils {
    private final Keyboard k1;//自定义键盘
    private KeyboardView keyboardView;
    private EditText editText;
    Huidiaojiekou huidiaojiekou;

    public interface Huidiaojiekou {
        public void huidiaojiekoufangfa();
    }

    public KeyboardView getKeyboardView() {
        return keyboardView;
    }



    public void setHuidiaojiekou(Huidiaojiekou huidiaojiekou) {
        this.huidiaojiekou = huidiaojiekou;
    }

    public KeyBoardUtils(KeyboardView keyboardView, EditText editText) {
        this.keyboardView = keyboardView;
        this.editText = editText;
        this.editText.setInputType(InputType.TYPE_NULL);//取消弹出系统键盘
        k1 = new Keyboard(this.editText.getContext(), R.xml.key);//将键盘数据同步到哪里 同步到edittext上面

        this.keyboardView.setKeyboard(k1);//设置要显示键盘的样式
        this.keyboardView.setEnabled(true); //可以使用
        this.keyboardView.setPreviewEnabled(false);//能进行预览
        this.keyboardView.setOnKeyboardActionListener(listener);//设置键盘按钮点击的监听
    }

    //设置键盘按钮点击的监听
    KeyboardView.OnKeyboardActionListener listener = new KeyboardView.OnKeyboardActionListener() {
        @Override
        public void onPress(int primaryCode) {

        }

        @Override
        public void onRelease(int primaryCode) {

        }

        @Override
        public void onKey(int primaryCode, int[] keyCodes) {
            Editable editable = editText.getText();
            int start = editText.getSelectionStart();
            switch (primaryCode) {
                case Keyboard.KEYCODE_DELETE://点击了删除键
                    if (editable != null && editable.length() > 0) {
                        if (start > 0) {
                            editable.delete(start - 1, start);
                        }
                    }
                    break;
                case Keyboard.KEYCODE_DONE://确定
                    if (editable != null && editable.length() > 0) {
                        huidiaojiekou.huidiaojiekoufangfa();// 通过接口回调的方法
                    } else {

                        Toast.makeText(editText.getContext(), "请输入金额!", Toast.LENGTH_SHORT).show(); // 提示用户必须输入
                    }
                    break;
                case Keyboard.KEYCODE_CANCEL://清零
                    editable.clear();
                    break;
                default://其他数字直接插入
                    editable.insert(start, Character.toString((char) primaryCode));
                    break;
            }

        }

        @Override
        public void onText(CharSequence text) {

        }

        @Override
        public void swipeLeft() {

        }

        @Override
        public void swipeRight() {

        }

        @Override
        public void swipeDown() {

        }

        @Override
        public void swipeUp() {

        }
    };

    //显示键盘
    public void showKeyboard() {
        int visibility = keyboardView.getVisibility();
        if (visibility == View.INVISIBLE || visibility == View.GONE) {
            keyboardView.setVisibility(View.VISIBLE);
        }
    }

    //隐藏键盘
    public void hideKeyboard() {
        int visibility = keyboardView.getVisibility();
        if (visibility == View.VISIBLE || visibility == View.INVISIBLE) {
            keyboardView.setVisibility(View.GONE);
        }
    }
}
